package com.basic.technology.study.introduction.thread.thread_pool;

import java.util.Collections;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

public class Queue {
    public static void main(String[] args) {
        SynchronousQueue<Integer> synchronousQueue = new SynchronousQueue<>();

        // 生产者线程
        new Thread(() -> {
            try {
                int value = 42;
                System.out.println("Producing: " + value);
                synchronousQueue.put(value);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        // 生产者线程
        new Thread(() -> {
            try {
                int value = 45;
                System.out.println("Producing: " + value);
                synchronousQueue.put(value);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        // 消费者线程
        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
                int consumedValue = synchronousQueue.take();
                System.out.println("Consuming: " + consumedValue);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

    }
}
